package com.quotes.charts;

import java.awt.*;
import java.util.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.*;
import com.quotes.datamodell.*;
import com.quotes.dates.TimeRange;
import com.quotes.indicators.*;

public class MacdChart extends LineChart
{
	private static final long serialVersionUID = 1L;
	private MacdIndicator macdIndicator = null;
	
	public MacdChart(Quotes quotes, TimeRange range, int macdShortTicks, int macdLongTicks, int signalLineTicks) {
		super(quotes, range);
		
		this.macdIndicator = new MacdIndicator(quotes, macdShortTicks, macdLongTicks, signalLineTicks);
		this.createChart(quotes);
		
		logger.debug("MacdChart created!");
	}
	
	protected XYDataset createXyDataset(Quotes quotes) {
		TimeSeriesCollection data = new TimeSeriesCollection();

    	data.addSeries(macdIndicator.getMacd().extract(range).convertToChartTimeSeries("MACD"));
    	data.addSeries(macdIndicator.getSingalLine().extract(range).convertToChartTimeSeries("SignalLine"));

		return data;
	}
	
	@Override
	protected XYPlot manipulatePlot(XYPlot plot) {
		XYItemRenderer renderer = plot.getRenderer();
		renderer.setSeriesPaint(0, Color.black);
		renderer.setSeriesPaint(1, Color.yellow);
		
		for (TimeRange range : macdIndicator.getMacdHistogramm().getPositiveRanges()) {
			addHorizontalMarker(plot, range, org.jfree.chart.ChartColor.LIGHT_GREEN);
	    }
		
		for (TimeRange range : macdIndicator.getMacdHistogramm().getNegativeRanges()) {
			addHorizontalMarker(plot, range, org.jfree.chart.ChartColor.LIGHT_RED);
	    }
		
		addVerticalMarker(plot, 0, Color.black);
		
		return plot;
	}
}
